﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using nyxwin.Main.Base.ViewModels;
using nyxwin.Common.Settings;
using nyxwin.Data.Mail;
using System.Collections.Generic;
using nyxwin.Data.Friends;
using nyxwin.Main.ViewModel.SubModels;
using System.Linq;

namespace nyxwin.Main.ViewModel.Main
{
    public class FriendsViewModel : PivotItemViewModel
    {
        private static readonly Guid GETFRIENDS_ID = Guid.NewGuid();

        private AppBarButton _refreshButton;

        public FriendsViewModel()
        {
            this.Friends = new List<FriendViewModel>();

            _refreshButton = new AppBarButton(new Uri("/Icons/refresh.png", UriKind.Relative), () => Refresh())
            {
                IsEnabled = true
            };

            ActualizeAppBarTitles();
        }

        protected override void OnLocalizationChanged()
        {
            base.OnLocalizationChanged();

            RaisePropertyChanged(() => Title);
            ActualizeAppBarTitles();
        }

        private void ActualizeAppBarTitles()
        {
            _refreshButton.Title = Localization.GetString("RefreshButtonTitle");
        }

        public override IEnumerable<AppBarButton> AppBarButtons
        {
            get
            {
                yield return _refreshButton;
            }
        }

        public override string Title
        {
            get
            {
                return Localization.GetString("FriendsTitle");
            }
        }

        private void Refresh()
        {
            SetIsInDeterminate(GETFRIENDS_ID, Localization.GetString("GettingFriends"));

            var friendsMgr = new FriendManager(ApplicationSettings.Instance.AuthorizationNick, ApplicationSettings.Instance.AuthorizationToken);
            friendsMgr.ErrorOccurred +=
                (evSender, evArgs) => Dispatcher.BeginInvoke(() =>
                {
                    UnSetInDeterminate(GETFRIENDS_ID);
                    RaiseError(evArgs.ErrorMessage);
                });

            friendsMgr.GetFriendsCompleted +=
                (evSender, evArgs) => Dispatcher.BeginInvoke(() =>
                {
                    if (evArgs.Error)
                        RaiseError(evArgs.ErrorMessage);
                    else
                    {
                        if(Friends != null)
                            foreach (var friend in Friends)
                            {
                                friend.Unload();
                            }

                        Friends = evArgs.FriendList.Select(x => new FriendViewModel(x)).ToList();
                    }

                    UnSetInDeterminate(GETFRIENDS_ID);
                });

            friendsMgr.GetFriends();
        }

        private List<FriendViewModel> _friends;

        public List<FriendViewModel> Friends
        {
            get { return _friends; }
            set
            {
                _friends = value;
                RaisePropertyChanged(() => Friends);
                RaisePropertyChanged(() => ActiveFriends);
                RaisePropertyChanged(() => NonActiveFriends);
            }
        }

        public List<FriendViewModel> ActiveFriends
        {
            get { return _friends.Where(x => x.Active != null).ToList(); }
        }

        public List<FriendViewModel> NonActiveFriends
        {
            get { return _friends.Where(x => x.Active == null).ToList(); }
        }

        public override void Enter()
        {
            Refresh();
        }
    }
}
